//
//  ST_FirstImpressionCountdownView.swift
//  btvideo
//
//  Created by Jason on 2021/12/21.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import RxSwift

/// 初体验倒计时
class ST_FirstImpressionCountdownView: UIView {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        addSubview(backgroundView)
//        backgroundView.layer.addSublayer(indicatorLayer)
        backgroundView.addSubview(indicator_image_view)
        addSubview(left_image_view)
        
        left_image_view.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(left_image_view.snp.height)
        }
        
        backgroundView.snp.makeConstraints { make in
            make.left.equalTo(left_image_view.snp.centerX)
            make.centerY.equalTo(left_image_view)
            make.height.equalTo(background_view_height)
            make.right.equalToSuperview()
        }
        
        indicator_image_view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(backgroundView)
            make.left.equalTo(backgroundView).offset(41)
        }
        
        addSubview(seconds_label)
        seconds_label.snp.makeConstraints { make in
            make.centerY.equalTo(backgroundView)
            make.right.equalTo(backgroundView).offset(-22)
        }
        
    }
    
    
    private var didAnimate = false
    
    private let animationDuration : Int = 10
    
    private var currentIndex = 0
    
    override func layoutSubviews() {
        super.layoutSubviews()

        
        if !didAnimate{
            
            
            UIView.animate(withDuration: 10) {
                self.indicator_image_view.transform = .identity
                
            } completion: { _ in
                
                self.timer?.invalidate()
                self.timer = nil
                self.removeFromSuperview()
                
            }

            
//            let animation = CABasicAnimation(keyPath: "bounds")
//            animation.toValue = CGRect(x: 0, y: 0, width: 259*2, height: background_view_height)
//            animation.duration = animationDuration.double
//            animation.fillMode = .forwards
//            animation.isRemovedOnCompletion = false
//            animation.delegate = self
//            indicatorLayer.add(animation, forKey: nil)
            
            
            backgroundView.roundCorners([.topRight,.bottomRight], radius: background_view_height/2.0)
            
            startTimer()
            
            didAnimate = true
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var left_image_view:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("impression_left_icon"))
        return v
    }()
    
    fileprivate lazy var indicator_image_view:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("impression_indicator_background_icon"))
        v.transform = CGAffineTransform.init(translationX: -259, y: 0)
//        v.transform = CGAffineTransform(scaleX: 0, y: 1)
//        v.layer.anchorPoint = CGPoint.init(x: 0, y: 0.5)
//        v.layer.transform = CATransform3D.init(scaleX: 0, y: 1, z: 1)
        return v
    }()
    
    
//    fileprivate lazy var indicatorLayer : CAGradientLayer = {
//        let bgLayer1 = CAGradientLayer()
//        bgLayer1.frame = CGRect(x: 0, y: 0, width: 0, height: background_view_height)
//        bgLayer1.colors = [UIColor(hex: 0x3dff5f5f),UIColor(hex: 0xffff5f5f),UIColor(hex: 0xfffec207)].compactMap{$0}.map{$0.cgColor}
//        bgLayer1.locations = [0, 0.5, 1]
//        bgLayer1.startPoint = CGPoint.init(x: 0, y: 0)
//        bgLayer1.endPoint = CGPoint.init(x: 1, y: 0)
//        return bgLayer1
//    }()
    
    private let background_view_height = 44.cgFloat
    
    fileprivate lazy var backgroundView : UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.init(hex: 0, transparency: 0.2)
        return v
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.text = "速配初印象"
        l.textColor = .white
        l.font = .systemFont(ofSize: 14, weight: .medium)
        return l
    }()
    
    fileprivate lazy var seconds_label:UILabel = {
        let l = UILabel()
        l.text = "0s"
        l.textColor = .white
        l.font = .systemFont(ofSize: 14, weight: .medium)
        return l
    }()
    
    private var timer:Timer?
}


extension ST_FirstImpressionCountdownView:CAAnimationDelegate{
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool){
        
        
        self.removeFromSuperview()
        
        timer?.invalidate()
        timer = nil
    }
    
    
    private func startTimer(){
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timer_handle), userInfo: nil, repeats: true)
        
    }
    
    @objc private func timer_handle(){
        
        currentIndex += 1
         
        seconds_label.text = currentIndex.string + "s"
        
    }
    
}
